package com.ygou.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ygou.pojo.Admininfo;
import com.ygou.pojo.Announcement;
import com.ygou.pojo.AnnouncementExample;
import com.ygou.pojo.Orders;
import com.ygou.pojo.AnnouncementExample.Criteria;
import com.ygou.service.AnnouncementService;
import com.ygou.utils.AppendContentToFile;

@Controller
@RequestMapping(value = "/annount")
@SessionAttributes(value = { "alist", "announce" })
public class AnnouncementController {
	@Autowired
	AnnouncementService announcementService;
	@Autowired
	HttpServletResponse response;

	// 根据条件查询
	@RequestMapping(value = "/selectByExample")
	public String selectByExample(@RequestParam(value="currPageNo",defaultValue="1") Integer currPageNo,
			@RequestParam(value="pageSize",defaultValue="10") Integer pageSize,
			HttpSession session,String title,ModelMap model) {
		PageHelper.startPage(currPageNo, pageSize);
		List<Announcement> list = announcementService.findByWhere(title,null);
		PageInfo<Announcement> pages=new PageInfo<Announcement>(list,pageSize);
		model.put("pageInfo", pages);
		return "/admin/article_Sort";
	}

	// 添加
	@RequestMapping(value = "/insertAnnouce",produces="application/json;charset=UTF-8")
	public @ResponseBody boolean insertUser(HttpSession session,Announcement annouce, HttpServletRequest request){
		/*******写入日志******/
		Admininfo admin= (Admininfo)session.getAttribute("admin");
		int id=0;
		if(admin!=null){
			id=admin.getAdminid();
		}
		AppendContentToFile.fileLogPrint(id+"", "对公告进行了添加");
		/*******END写入日志******/
		return announcementService.insert(annouce);
	}

	// 根据主键查询
	@RequestMapping(value = "/selectByPrimaryKey")
	public String selectByPrimaryKey(HttpSession session,Integer annid, ModelMap model,int flag) {
		if(annid!=null){
			Announcement announce = announcementService.selectByPrimaryKey(annid);
			model.put("announce", announce == null ? "" : announce);
			List<Announcement> alist = announcementService.findByWhere(null,annid);
			model.put("alist",alist);
		}
		if (flag == 1) {
			return "/admin/article_upd";//后台的修改
		}else if(flag==2){
			Admininfo admin= (Admininfo)session.getAttribute("admin");
			int id=0;
			if(admin!=null){
				id=admin.getAdminid();
			}
			AppendContentToFile.fileLogPrint(id+"", "对公告进行了查看详细信息");
			return "/admin/article_show";
		}else if(flag==3){
			List<Announcement> list = announcementService.selectByExample();
			if(annid==null){
				Announcement announce=list.get(0);
				model.put("announce", announce);
			}
			model.put("list", list);
			return "blog";//前台公告页面
		}
		return null;
	}

	// 修改
	@RequestMapping(value = "/updateByPrimaryKey", produces = "application/json;charset=UTF-8")
	public @ResponseBody boolean updateByPrimaryKey(HttpSession session,Announcement announce) {
		Announcement a=announcementService.selectByPrimaryKey(announce.getAnnid());
		a.setAnnid(announce.getAnnid());
		a.setAnntitle(announce.getAnntitle());
		a.setAnncontent(announce.getAnncontent());
		/*******写入日志******/
		Admininfo admin= (Admininfo)session.getAttribute("admin");
		int id=0;
		if(admin!=null){
			id=admin.getAdminid();
		}
		AppendContentToFile.fileLogPrint(id+"", "对"+a.getAnnid()+"号公告进行了修改");
		/*******END写入日志******/
		return announcementService.updateByPrimaryKey(a);
	}

	// 删除
	@RequestMapping(value = "/deleteByPrimaryKey")
	public  @ResponseBody boolean deleteByPrimaryKey(HttpSession session,Integer annid) throws IOException {
		/*******写入日志******/
		Admininfo admin= (Admininfo)session.getAttribute("admin");
		int id=0;
		if(admin!=null){
			id=admin.getAdminid();
		}
		AppendContentToFile.fileLogPrint(id+"", "删除了"+annid+"公告");
		/*******END写入日志******/
		return announcementService.deleteByPrimaryKey(annid);
	}
	
	//批量删除
	@RequestMapping(value = "/deleteByAll")
	public String deletes(HttpSession session,String[] dels){
		if(dels==null){
			
		}else{
			for (String str : dels) {
				/*******写入日志******/
				Admininfo admin= (Admininfo)session.getAttribute("admin");
				int id=0;
				if(admin!=null){
					id=admin.getAdminid();
				}
				AppendContentToFile.fileLogPrint(id+"", "进行了批量删除"+str+"公告");
				/*******END写入日志******/
				announcementService.deleteByPrimaryKey(Integer.parseInt(str));
			}
		}
		
		return "/annount/selectByExample?flag=2";
	}
}
